package com.yq.cp.service.yw.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.MyException;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.yw.YwGdxxGdlx;
import com.yq.cp.mapper.yw.YwGdxxGdlxMapper;
import com.yq.cp.service.yw.YwGdxxGdlxService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 运维-工单-工单类型 服务实现类
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@Service
@Transactional
public class YwGdxxGdlxServiceImpl extends ServiceImpl<YwGdxxGdlxMapper, YwGdxxGdlx> implements YwGdxxGdlxService {

    @Resource
    YwGdxxGdlxMapper ywGdxxGdlxMapper;
    @Override
    public void add(YwGdxxGdlx ywGdxxGdlx) {
        QueryWrapper<YwGdxxGdlx> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",ywGdxxGdlx.getName());
        boolean exists = ywGdxxGdlxMapper.exists(queryWrapper);
        if (exists){
            throw new MyException("名称重复");
        }
        ywGdxxGdlxMapper.insert(ywGdxxGdlx);
    }

    @Override
    public void update(YwGdxxGdlx ywGdxxGdlx) {
        YwGdxxGdlx ywGdxxGdlx1 = ywGdxxGdlxMapper.selectById(ywGdxxGdlx.getId());
        if (ywGdxxGdlx1!=null){

            QueryWrapper<YwGdxxGdlx> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("name",ywGdxxGdlx.getName());
            queryWrapper.notIn("id",ywGdxxGdlx.getId());
            boolean exists = ywGdxxGdlxMapper.exists(queryWrapper);
            if (exists){
                throw new MyException("名称重复");
            }

            ywGdxxGdlx1.setName(ywGdxxGdlx.getName());
            ywGdxxGdlx1.setRankNumber(ywGdxxGdlx.getRankNumber());
            ywGdxxGdlxMapper.updateById(ywGdxxGdlx1);
        }
    }

    @Override
    public void sc(YwGdxxGdlx ywGdxxGdlx) {
        if (ywGdxxGdlx.getId().compareTo(100l)==-1){
            throw new MyException("不能删除默认类型");
        }
        ywGdxxGdlxMapper.deleteById(ywGdxxGdlx.getId());
    }

    @Override
    public IPage<YwGdxxGdlx> page(PageVo vo) {
        QueryWrapper<YwGdxxGdlx> queryWrapper=new QueryWrapper<>();
        if (vo.getLike()!=null){
            queryWrapper.like("name",vo.getLike());
        }
        queryWrapper.orderByAsc("rank_number");
        IPage<YwGdxxGdlx> ipage=new Page<>(vo.getPage(),vo.getSize());
        ywGdxxGdlxMapper.selectPage(ipage,queryWrapper);
        return ipage;
    }
}
